//引入组件库
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var dialog = sm("do_Dialog");
var userUrl = require("url/user");
var circleUrl = require("url/circle");
var imHelper = require("helper/im");
var userUtil = require("helper/user");
var http = require("helper/http");
var hashdata = mm("do_HashData");
//声明UI变量
var listview=ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据

var httpFriendsUrl = userUrl.url.friend.myFriendList;//我的好友
var httpCreate4ChatUrl = circleUrl.url.base.create4chat;//发起群聊
/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	app.closePage();
});

//右上角-确定
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "确定";
headerRight_lableNext.visible = true;
headerRight.on("touch", "", 5000, function() {
	var selectFriendIds = [];
	for(var i=0;i<listdata.getCount();i++){
		oneData = listdata.getOne(i);
		if(oneData.checked){
			selectFriendIds.push(oneData.friendId);
		}
	}
	if(selectFriendIds.length<=1){
		nf.toast("至少选择两位好友!");
		return;
	}
	//deviceone.print(selectFriendIds,"selectFriendIds");
	var data1 = {title : "群聊名称",	hint : "输入群聊名称"}
	dialog.open("source://view/reuse/popInput/pop_input.ui", data1, function(returnText, e) {
		if (returnText){
			creat4gchat(selectFriendIds,returnText);
		}else{
			nf.toast("请输入 群聊名称");
		}
	});
});


var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "选择我的好友--发起群聊"
})
header.refreshData();

/****************处理页面-头部-end*****************/
//andriod返回键
page.on("back", function() {
	app.closePage();
});

var myNick = "";//我的呢称
page.on("loaded", function() {
	getMyInfo();
	loadData();
});

function getMyInfo() {
	imHelper.loadMe(function(myImUser) {
		myNick = myImUser.name;
		deviceone.print("myNick:"+myNick);
	})
}

//更改选中状态
page.on("friend_check_change_event", function(friend) {
	//deviceone.print("friend_check_change_event");
	var oneData = null;
	for(var i=0;i<listdata.getCount();i++){
		oneData = listdata.getOne(i);
		if(oneData.friendId == friend.friendId){
			oneData.checked = friend.checked;
			listdata.updateOne(i, oneData);//全部设为“不选中”或"选中"
		}
	}
	//listview.refreshItems();
})

var page = 1;
function loadData(){
	pageNum = 1;
	var params = {
		page : pageNum		
	}
	http.post(httpFriendsUrl,params,function(data){
		if(data.friendPage){
			var friendList = data.friendPage.content;
			listdata.removeAll();
			for(var i=0;i<friendList.length;i++){
				friendList[i].checked = false;//设为“不选中”
			}
			listdata.addData(friendList);
			listview.refreshItems();
		}
	});
}
function getNextPageData() {
	pageNum++;
	var params = {
		page : pageNum
	}
	http.post(httpFriendsUrl, params, function(data) {
		if(data.friendPage){
			var friendList = data.friendPage.content;
			for(var i=0;i<friendList.length;i++){
				friendList[i].checked = false;//设为“不选中”
			}
			listdata.addData(friendList);
			listview.refreshItems();
		}
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data) {
	if (data.state == 2) {
		loadData();
		listview.rebound();//复位【一定要做】
	}
});

// 上拉列表，翻页数据
listview.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
		listview.rebound();//复位【一定要做】
	}
});

//发起群聊
function creat4gchat(memberIdsList,groupChatName){
	if(groupChatName==null || groupChatName.length==0){
		nf.toast("请输入 群聊名称");
		return;
	}
	var memberIdsString = memberIdsList.join(';');//成员列表字符串
	var params = {"groupChatName":groupChatName,"memberIds":memberIdsString};
	http.post(httpCreate4ChatUrl, params, function(data) {
		if(data.circleId){//得到群组(圈子)id
			//deviceone.print(circleId,"circleId4chat")
			listdata.removeAll();//跳到群聊前清空好友页面列表
			listview.refreshItems();
			imHelper.sendGroupChatTip(data.circleId, "GroupChatTip:\""+myNick+"\" 邀请你加入群聊");
			imHelper.toGroupChat(data.circleId);
		}
	});
	
}